package com.cap.bts.crmdp.dealer.coupon.dao.impl;

import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.activation.domain.PackageRedemptionLines;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao;
import com.cap.bts.crmdp.dealer.coupon.domain.TicketLine;
import com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto;
import com.cap.bts.framework.common.dao.callback.QueryJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;

@Repository("iTicketLineDefineDao")
public class TicketLineDefineDaoImpl extends BaseDaoImpl<TicketLine, Long> implements
                                                                          ITicketLineDefineDao {

    @Inject
    public TicketLineDefineDaoImpl(@Value("com.cap.bts.crmdp.dealer.coupon.domain.TicketLine") Class<TicketLine> clazz) {
        super(clazz);
    }

    @Override
    public TicketLine insertTicketLineInfo(TicketLine ticketLine) throws AppException {
        return save(ticketLine);

    }

    /**
     * 根据优惠券和提货券ID查询其详细信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao#getTicketLineInfoList(java.lang.String)
     */
    @Override
    public List<TicketLine> getTicketLineInfoList(String ticketId) throws AppException {
        String selectStr = "select * from tm_raw_ticket_line_def where ticket_def_id=?1";
        return super.getListByNativeQueryWithVaParam(selectStr, ticketId);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 14, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao#getTicketDescriptionInfoByTicketId(java.lang.Long)
    **/
    @SuppressWarnings("unchecked")
    @Override
    public String getTicketDescriptionInfoByTicketId(Long ticketDefineId) throws AppException {
        List<String> descriptionList = super
            .getJpaTemplate()
            .getEntityManagerFactory()
            .createEntityManager()
            .createNativeQuery(
                "SELECT GROUP_CONCAT(e.DESCRIPTION) AS DESCRIPTION FROM (SELECT d.TICKET_DEF_ID AS TICKET_DEF_ID, CONCAT(d.ITEM_NAME,'*',d.QUANTITY) AS DESCRIPTION FROM  tm_raw_ticket_line_def d) e WHERE e.TICKET_DEF_ID=?1 GROUP BY e.TICKET_DEF_ID")
            .setParameter(1, ticketDefineId).getResultList();
        if (descriptionList != null && !descriptionList.isEmpty()) {
            return descriptionList.get(0);
        }
        return null;
    }

    /**
     * 根据优惠券和提货券的详细信息的ID查询其详细信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao#getTicketLineInfo(java.lang.Long)
     */
    @Override
    public TicketLine getTicketLineInfoById(Long id) throws AppException {
        String selectStr = "select * from tm_raw_ticket_line_def where id=?1";

        TicketLine ticketLine = null;

        List<TicketLine> list1 = super.getListByNativeQueryWithVaParam(selectStr, id);
        if (!list1.isEmpty()) {
            ticketLine = list1.get(0);
        }
        return ticketLine;
    }

    /**
     * 优惠券和提货券的详细信息修改
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao#updateTiketLineInfo(com.cap.bts.crmdp.dealer.coupon.dto.TicKetDefineDto)
     */
    @Override
    public boolean updateTiketLineInfo(TicKetDefineDto ticKetDefineDto) throws AppException {
        TicketLine ticketLine = findById(ticKetDefineDto.getId());
        ticketLine.setTicketDefId(ticKetDefineDto.getTicketDefId());
        ticketLine.setItemName(ticKetDefineDto.getItemName());
        ticketLine.setPrepayAmount(ticKetDefineDto.getPrepayAmount());
        ticketLine.setQuantity(ticKetDefineDto.getQuantity());

        update(ticketLine);
        return true;
    }

    /**
     * 根据ticketDefId删除优惠券和提货券的详细信息
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao#removeTicketLineInfoByTicketDefId(java.lang.Long)
     */
    @SuppressWarnings("rawtypes")
    @Override
    public boolean removeTicketLineInfoByTicketDefId(List<TicketLine> ticketLineList)
                                                                                     throws AppException {
        for (Iterator iterator = ticketLineList.iterator(); iterator.hasNext();) {
            TicketLine ticketLine = (TicketLine) iterator.next();
            remove(ticketLine);
        }
        return true;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 27, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ITicketLineDefineDao#getPackageRedemptionLinesByTicketDefId(java.lang.Long)
    **/
    @Override
    public PackageRedemptionLines getPackageRedemptionLinesByTicketDefId(Long ticketDefId) throws AppException {
        String hql = "from PackageRedemptionLines a where  a.ticket.id="+ ticketDefId;
        List<PackageRedemptionLines> list=this.getJpaTemplate().execute(new QueryJpaCallback<PackageRedemptionLines>(hql));
        if(list!=null&&!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

}
